//
//  WBBaseViewController.swift
//  daoge_sineweibo
//
//  Created by 伍禄林 on 16/9/26.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController{
    
   
    
    var tableView: UITableView?
    
    var isPull: Bool = false
    
    var refershControl: CZRefreshControl?
    
    var visitroInfo: [String: String]?

    lazy var naviBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    
    
    lazy var naviItem: UINavigationItem = UINavigationItem();
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if WBNetworkingManager.shared.userLogon {
           loadData()
        }
        
        setUpUi()
        //取消tableview或者scrollerVeiw的缩进
        automaticallyAdjustsScrollViewInsets = false
        
        
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name: NSNotification.Name(rawValue:WBUserLoginSuccessNotification), object: nil)
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    func loadData()  {
        refershControl?.endRefreshing()
    }
    
    override var title: String? {
        
        didSet{
            naviItem.title = title
        }
    }
    
}

extension WBBaseViewController {
    
    func loginSuccess()  {
        
        print("登陆成功")
        
        naviItem.leftBarButtonItem = nil
        naviItem.rightBarButtonItem = nil
        
        self.viewDidLoad()
        
        NotificationCenter.default.removeObserver(self)
        
    }
    
    func login()  {
    
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBUserShouldLoginNotification), object: nil)
        
    }
    
    func register() {
        print("用户注册")
    }
    
}

extension WBBaseViewController {
    
    func setUpUi() {
        view.backgroundColor = UIColor.white
        setupNaviBar()
        WBNetworkingManager.shared.userLogon ? setupTableView() : setupVisitorView()
    }
    
    private func setupVisitorView() {
        
        let v = WBVisitorView(frame: view.bounds)
        view.insertSubview(v, belowSubview: naviBar)
        v.loginBtn.addTarget(self, action: #selector(login), for: .touchUpInside)
        v.registerBtn.addTarget(self, action: #selector(register), for: .touchUpInside)
        v.visitorInfo = visitroInfo
        naviItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        naviItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: .plain, target: self, action: #selector(login))
    }
    
    func setupTableView() {
        
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: naviBar)
        tableView?.dataSource = self
        tableView?.delegate = self
        tableView?.contentInset = UIEdgeInsets(top: naviBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        tableView?.scrollIndicatorInsets = tableView!.contentInset
        
        refershControl = CZRefreshControl()
        tableView?.addSubview(refershControl!)
        refershControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    private func setupNaviBar() {
        view.addSubview(naviBar)
        naviBar.items = [naviItem]
        //设置整个导航条的背景颜色
        naviBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        //设置title的字体颜色
        naviBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray]
        //设置导航条Button的颜色
        naviBar.tintColor = UIColor.orange
    }
    
}

extension WBBaseViewController: UITableViewDataSource,UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        
        let section = tableView.numberOfSections - 1
        
        if row<0 || section < 0 {
            
            return
        }
        
        let count = tableView.numberOfRows(inSection: section)
        
        
        print("row======\(row)====selction=======\(section)====count=====\(count)")
        
        if row == (count - 1) && !isPull {
            
            print("上拉刷新")
            isPull = true
            loadData()
        }
        
    }
}

